package cn.com.v2.controller;

import cn.com.v2.model.GoviewProjectData;
import cn.com.v2.model.SysFile;
import cn.com.v2.service.ISysFileService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.SQLException;

@RestController
@RequestMapping("/oss")
public class OssController {

    @Resource
    private ISysFileService iSysFileService;

    @GetMapping("/{date}/{fileName}")
    public ResponseEntity<byte[]> getImageFromDbById(@PathVariable String date,@PathVariable String fileName) throws IOException, SQLException {
        SysFile sysFile = iSysFileService.getOne(
                new LambdaQueryWrapper<SysFile>()
                        .eq(SysFile::getFileName, fileName));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_JPEG); // 根据实际情况调整媒体类型
        headers.setContentLength(sysFile.getFileContent().length);
        return new ResponseEntity<>(sysFile.getFileContent(), headers, HttpStatus.OK);
    }
}
